<?php
/* @var $this PostController */
/* @var $model Post */

$this->breadcrumbs=array(
	'Posts'=>array('index'),
	'Manage',
);

Yii::app()->clientScript->registerScript('search', "
$('.search-button').click(function(){
	$('.search-form').toggle();
	return false;
});
$('.search-form form').submit(function(){
	$('#post-grid').yiiGridView('update', {
		data: $(this).serialize()
	});
	return false;
});
");
?>

<div class="page-header">
	<div class="page-title"><h3>Manage Posts</h3></div>
	<div class="buttons">
		<a href="javascript:void(0)" class="search-button btn">Advanced Search</a>
		<a href="<?php echo $this->createurl('create')?>" class="btn btn-primary">Add Post</a>
	</div>
</div>

<div class="search-form" style="display:none">
<?php $this->renderPartial('_search',array(
	'model'=>$model,
)); ?>
</div><!-- search-form -->

<div class="row-fluid">
	<div class="mass-actions-form">
	<?php $form=$this->beginWidget('CActiveForm', array(
		'id'=>'massActionsForm',
		'enableClientValidation' => true,
		'clientOptions'=> array('validateOnSubmit'=>true),
		'action'=>$this->createUrl('massActions'),
		'htmlOptions'=>array('class'=>'form-inline'),
	)); ?>
		<?php echo CHtml::hiddenField('items','',array('id'=>'massActionItems')) ?>

		<p class="pull-right">
			<span class="pull-left">Action:</span>
			<?php echo CHtml::dropDownList('action', '', array(0=>'', 'update'=>'Update', 'delete'=>'Delete'), array('class'=>'actions')); ?>
			<?php echo CHtml::submitButton('Submit', array('class'=>'btn btn-danger')) ?>
		</p>
	<?php $this->endWidget(); ?>
	</div>
</div>

<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'post-grid',
	'dataProvider'=>$model->search(),
	'formatter'=>new BlogFormatter(),
	'columns'=>array(
		// checkbox
		array('class'=>'CCheckBoxColumn', 'name'=>'id', 'selectableRows'=>2, 'headerHtmlOptions'=>array('style'=>'width: 30px'), 'checkBoxHtmlOptions'=>array('class'=>'massActionCheckbox')),
		
		// id
		array('name'=>'id', 'headerHtmlOptions'=>array('style'=>'width: 50px')),

		// title
		array('name'=>'title'),

		// status
		array('name'=>'status', 'type'=>'postStatus',
			'htmlOptions'=>array('class'=>'hidden-phone'), 'headerHtmlOptions'=>array('class'=>'hidden-phone'), 'filterHtmlOptions'=>array('class'=>'hidden-phone')
		),

		// category
		array('name'=>'category_id', 'value' => '$data->category? $data->category->name : null',
			'htmlOptions'=>array('class'=>'hidden-phone'), 'headerHtmlOptions'=>array('class'=>'hidden-phone'), 'filterHtmlOptions'=>array('class'=>'hidden-phone')
		),

		// visibility
		array('name'=>'visibility', 'type'=>'postVisibility',
			'htmlOptions'=>array('class'=>'hidden-phone'), 'headerHtmlOptions'=>array('class'=>'hidden-phone'), 'filterHtmlOptions'=>array('class'=>'hidden-phone')
		),

		// updated time
		array('name'=>'updated_time', 'type'=>'shortDate', 'filter'=>false,
			'htmlOptions'=>array('class'=>'hidden-tablet hidden-phone'), 'headerHtmlOptions'=>array('class'=>'hidden-tablet hidden-phone'), 'filterHtmlOptions'=>array('class'=>'hidden-tablet hidden-phone')
		),
		
		// actions
		array(
			'header'=>'Actions',
			'class'=>'CButtonColumn',
			'template'=>'{update} {delete}',
			'headerHtmlOptions'=>array('class'=>'span2')
		),
	),
	'itemsCssClass'=>'table table-bordered table-hover',
	'cssFile'=>false,
)); ?>

<script type="text/javascript">
//<![CDATA[
$('#massActionsForm').submit(function(e) {
	if (this.action.value=="0") {
		alert('Please select action');
		e.preventDefault();
		return;
	}

	var items = getSelectedItems();
	if (!items.length) {
		alert('Please select items by clicking checkbox');
		e.preventDefault();
		return;
	}
	
	this.items.value = items.join('_');
});

function getSelectedItems() {
	var result = [];
	$('.massActionCheckbox:checked').each(function () {
		result.push(this.value);
	});
	return result;
}
//]]>
</script> 